<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Mfinancials_model extends MY_Model {
	
	protected $m_financials;
	
	function __construct() {
		parent::__construct();
	}
	
	function table_name() {
		return "m_financials";
	}
	
	function get_objects($filters = array(), $counting = false) {
		if (isset($this->CI->cache)) {
			$caching = true;
			$cache_key = 'ci--M_FINANCIAL_'.md5(serialize($filters));
			
			if ($counting) {
				$cache_key = 'ci--M_FINANCIAL_COUNT_'.md5(serialize($filters));
			}
			
			if ($return = $this->cache->file->get($cache_key)) {
				return ($return == 'empty_cache') ? FALSE : $return;
			}
		} else {
			$caching = false;
		}
		
		
		$this->db->select('m_financials.*,cc.name AS category_name,cc.alias AS category_alias')
				->join('categories AS cc','cc.category_id = m_financials.category_id','left')
				->from('m_financials');
		$this->db->where('m_financials.is_delete', 0);
				
		if (isset($filters['id'])) {
			$this->db->where('m_financials.id', $filters['id']);
		}

		if (isset($filters['alias'])) {
			$this->db->where('m_financials.alias', $filters['alias']);	
		}
		
		if (isset($filters['category_id'])) {
			$this->db->where('m_financials.category_id', $filters['category_id']);
		}
		
		if (isset($filters['name'])) {
			$this->db->like('m_financials.name', $filters['name']);
		}
		
		if (isset($filters['status'])) {
			$this->db->where('m_financials.status', $filters['status']);
		}
		
		if (isset($filters['limit'])) {
			$offset = (isset($filters['page'])) ? $filters['page'] : 0;
			$this->db->limit($filters['limit'], $offset);
		}
		
		$this->db->order_by('m_financials.id','DESC');
		
		$result = $this->db->get();
		if ($counting !=false) {
			$rows = $result->num_rows();
			$result->free_result();
			
			if ($caching == TRUE) {
				$this->CI->cache->file->save($cache_key, $rows, (5*60));	
			}
			
			return $rows;
		}
		if ($result->num_rows() == 0) {
			return false;
		}
		
		$list = array();
		foreach ($result->result_array() as $row) {
			$row['link_type'] = create_url($row['category_alias'] .'/'. $row['alias'], array('rewrite'=>true));
			$list[] = $row;
		}

		if ($caching) {
			$this->CI->cache->file->save($cache_key, $list);
		}
		$result->free_result();
		
		return $list;
	}
	
	function get_object_by_id( $id ) {
		if (!isset($this->m_financials[$id])) {
			$object = $this->get_objects( array('id' => $id));
			$this->m_financials[$id] = $object;
		} else {
			$object = $this->m_financials[$id];
		}
		if (empty($object)) {
			return false;
		}
		return $object[0];
	}
	
	function get_object_by_alias($type_alias) {
		$filters = array('alias' => $type_alias);
		$result = $this->get_objects($filters);
		if (empty($result)) {
			return false;
		}
		return $result[0];
	}
	
	function get_objects_options() {
		$object_options = array();
		$list = $this->get_objects(array());
		if (is_array($list) && count($list) > 0) {
			foreach ($list as $value) {
				$object_options[$value['id']] = $value['name'];
			}
		}
		return $object_options;
	}
	
	function get_objects_count($filters = array()) {
		return $this->get_objects($filters, true);
	}
	
	function insert($data) {
		$data['category_id'] = 33;
		$this->save($data);
		return true;
	}
	
	function update($attributes, $id) {
		return parent::update($attributes, array('id'=>$id));
	}
	
	function delete($id) {
		return parent::update(array('is_delete' => 1), array('id' => $id));
	}
	
	function change_status($id, $change = 1) {
        return parent::update(array('status' => $change), array('id' => $id));
    }
}
